<?php
/**
 * Created by PhpStorm.
 * User: openoter
 * Date: 2018/12/9
 * Time: 21:35
 */

namespace app\admin\controller;


use app\admin\model\Cadre;
use app\admin\model\Cls;
use app\admin\model\Teacher;
use think\Db;
use think\Request;

class ClassesController extends BaseController {

    public function index() {
        $cls = Cls::with('head_teachers')->paginate(10);
//        halt(collection($cls)->toArray());
//        return json($cls);
        $this->assign('cls', $cls);
        return view('index');
    }

    /**
     * 添加
     * @param Request $request
     * @return \think\response\View|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(Request $request) {
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Cls');
            if (true !== $result)
                return $this->error($result, 'add');
            $param['create_time'] = $param['update_time'] = time();
            $res = Cls::create($param);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        } else {
            $teacher = Teacher::field(['id', 'name', 'zhiwu'])->select();
//            dump(collection($teacher)->toArray());
            $this->assign('teachers', $teacher);
            return view('add');
        }
    }

    public function edit(Request $request, $id) {
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Cls');
            if (true !== $result)
                return $this->error($result, 'add');
            $param['update_time'] = time();
            $res = Cls::update($param, ['id'=>$id]);
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'add');
            }
        } else {
            $cls = Cls::get($id);
            $this->assign('cls', $cls);
            $teacher = Teacher::field(['id', 'name', 'zhiwu'])->select();
//            dump(collection($teacher)->toArray());
            $this->assign('teachers', $teacher);
            return view('edit');
        }
    }

    public function status(Request $request){
        $param = $request->post();
        $cls = Cls::get($param['id']);
        $cls->status = $param['status'];
        $res = $cls->isUpdate(true)->save();
        if ($res) {
            return json(['message' => "修改成功", 'status' => 200]);
        } else {
            return json(['message' => '修改失败', 'status' => 400]);
        }
    }

    public function teacher($id) {
        $res = Db::query("select *  from t_teacher where find_in_set($id, class_ids)");
        $this->assign('teachers', $res);
        return view('teacher');
    }

    public function cadre($id) {
        $cadre = Cadre::where('class', $id)->select();
        $this->assign('cadre', $cadre);
        return view('cadre');
    }
}